<template>
  <div style="max-width: 600px; margin: 40px auto;">
    <el-card shadow="hover">
      <template #header>
        <div style="display: flex; justify-content: space-between; align-items: center;">
          <span style="font-size: 20px; font-weight: bold;">个人余额</span>
          <span style="color: #409EFF; font-weight: bold;">当前余额：￥{{ userStore.balance.toFixed(2) }}</span>
        </div>
      </template>
      <div style="margin-bottom: 24px;">
        <el-button type="primary" @click="showRecharge = true">模拟充值</el-button>
      </div>
      <el-table :data="userStore.balanceLogs" style="width: 100%" v-if="userStore.balanceLogs.length">
        <el-table-column prop="time" label="时间" width="160" />
        <el-table-column prop="type" label="类型" width="80" />
        <el-table-column prop="amount" label="金额" width="100">
          <template #default="scope">
            <span :style="{color: scope.row.amount > 0 ? '#67C23A' : '#F56C6C'}">
              {{ scope.row.amount > 0 ? '+' : '' }}{{ scope.row.amount.toFixed(2) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
      </el-table>
      <div v-else style="text-align: center; color: #bbb; padding: 40px;">暂无余额变动记录</div>
    </el-card>
    <!-- 模拟充值弹窗 -->
    <el-dialog v-model="showRecharge" title="模拟充值" width="400px">
      <el-form :model="rechargeForm" label-width="80px">
        <el-form-item label="金额">
          <el-input-number v-model="rechargeForm.amount" :min="1" :max="10000" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="rechargeForm.remark" placeholder="如：支付宝充值" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showRecharge = false">取消</el-button>
        <el-button type="primary" @click="onRecharge">充值</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/userStore'
const userStore = useUserStore()
const showRecharge = ref(false)
const rechargeForm = reactive({ amount: 100, remark: '' })
function onRecharge() {
  if (rechargeForm.amount > 0) {
    userStore.recharge(rechargeForm.amount, rechargeForm.remark || '模拟充值')
    ElMessage.success('充值成功！')
    showRecharge.value = false
  }
}
</script> 